package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.DishDao;
import com.oocl.dao.impl.DishDaoImpl;
import com.oocl.pojo.Dish;
import com.oocl.pojo.DishSearchCondition;
import com.oocl.service.DishManager;

public class DishManagerImpl implements DishManager{
	private DishDao dishDao;
	
	public DishManagerImpl(){
		dishDao = new DishDaoImpl();
	}
	
	@Override
	public int addDish(Dish d) {
		int n = dishDao.addDish(d);
		return n;
	}

	@Override
	public int updateDish(Dish d) {
		int n = dishDao.updateDish(d);
		return n;
	}

	@Override
	public int deleteDish(String dId) {
		int n = dishDao.deleteDish(dId);
		return n;
	}

	@Override
	public List<Dish> findDishes(Integer dType, String dName, String mId) {
		List<Dish> dl = null;
		if(dType==0){
			if(dName==null)dl=dishDao.findAllDishes(mId);
			else dl=dishDao.findDishesByName(dName, mId);
		}
		else {
			if(dName==null)dl=dishDao.findDishesByType(dType, mId);
			else dl=dishDao.findDishesByBoth(dType, dName, mId);
		}
		return dl;
	}


	@Override
	public Dish findDishById(String dId) {
		Dish dish = dishDao.findDishById(dId);
		return dish;
	}

	@Override
	public List<Dish> findDishByCondition(DishSearchCondition condition) {
		if(null==condition){
			return new ArrayList<>();
		}
		String sql = "select * from dish";
		boolean isWhere=false;
		if(null!=condition.getDishType()){
			sql += " where dtype="+condition.getDishType()+"";
			isWhere = true;
		}
		if(null!=condition.getDishName()){
			if(isWhere){
				sql += " and dname like'%"+condition.getDishName()+"'";
			}else {
				sql += " where dname like '%"+condition.getDishName()+"%'";
				isWhere = true;
			}
		}
		if(null!=condition.getmId()){
			if(isWhere){
				sql += " and mid='"+condition.getmId()+"'";
			}else {
				sql += " where mid='"+condition.getmId()+"'";
				isWhere = true;
			}
		}
		System.out.println("sqlon==>"+sql);
		return dishDao.findDishByCondition(sql);
	}

}
